package swing;

import redistool.RedisTool;
import utils.PropertiesUtil;
import utils.RedisUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Title: 新建连接JFrame
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class NewConnectionFrame extends JFrame {

    private RedisTool redisTool;
    private Container contentPane = this.getContentPane();
    private static JPanel namePanel = new JPanel();
    private static JPanel hostPanel = new JPanel();
    private static JPanel portPanel = new JPanel();
    private static JPanel authPanel = new JPanel();
    private static JLabel nameLabel = new JLabel("Name:", JLabel.LEFT);
    private static JLabel hostLabel = new JLabel("Host:", JLabel.LEFT);
    private static JLabel portLabel = new JLabel("Port:", JLabel.LEFT);
    private static JLabel authLabel = new JLabel("Auth:", JLabel.LEFT);
    private static JTextField nameField = new JTextField(25);
    private static JTextField hostField = new JTextField(25);
    private static JTextField portField = new JTextField(25);
    private static JTextField authField = new JTextField(25);
    private static JButton testBtn = new JButton("测试");
    private static JButton saveBtn = new JButton("保存");
    private static Color bgColor = new Color(67,67,67);
    private static Color inputColor = new Color(191,191,191);
    private static Font font = new Font("Menu.font", Font.PLAIN, 15);

    /**
     * 构造方法
     */
    public NewConnectionFrame(RedisTool redisTool) throws IOException {
        this.redisTool = redisTool;
        //初始化组件
        init();
        //初始化按钮
        initBtn();
        this.setTitle("新建连接");
        this.setIconImage(ImageIO.read(new File("resources/logo.png")));
        int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
        this.setLocation((screenWidth-300)/2, (screenHeight-300)/2);
        this.setSize(300,300);
        this.setVisible(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * 初始化组件
     */
    private void init(){
        contentPane.setLayout(null);
        namePanel.setBounds(0,5,290,40);
        hostPanel.setBounds(0,50,290,40);
        portPanel.setBounds(0,95,290,40);
        authPanel.setBounds(0,140,290,40);
        nameLabel.setFont(font);
        hostLabel.setFont(font);
        portLabel.setFont(font);
        authLabel.setFont(font);
        nameLabel.setForeground(Color.WHITE);
        hostLabel.setForeground(Color.WHITE);
        portLabel.setForeground(Color.WHITE);
        authLabel.setForeground(Color.WHITE);
        namePanel.setBackground(bgColor);
        hostPanel.setBackground(bgColor);
        portPanel.setBackground(bgColor);
        authPanel.setBackground(bgColor);
        nameField.setBackground(inputColor);
        hostField.setBackground(inputColor);
        portField.setBackground(inputColor);
        authField.setBackground(inputColor);
        nameField.setForeground(Color.WHITE);
        hostField.setForeground(Color.WHITE);
        portField.setForeground(Color.WHITE);
        authField.setForeground(Color.WHITE);
        namePanel.add(nameLabel);
        namePanel.add(nameField);
        hostPanel.add(hostLabel);
        hostPanel.add(hostField);
        portPanel.add(portLabel);
        portPanel.add(portField);
        authPanel.add(authLabel);
        authPanel.add(authField);
        testBtn.setBounds(60,200,60,30);
        saveBtn.setBounds(170,200,60,30);
        contentPane.add(namePanel);
        contentPane.add(hostPanel);
        contentPane.add(portPanel);
        contentPane.add(authPanel);
        contentPane.add(testBtn);
        contentPane.add(saveBtn);
        contentPane.setBackground(bgColor);
    }

    /**
     * 初始化按钮
     */
    private void initBtn(){

        //测试按钮监听器
        testBtn.addActionListener(e -> {
            String msg = "连接失败！";
//            String name = nameField.getText();
            String host = hostField.getText();
            String port = portField.getText();
            String auth = authField.getText();
            RedisUtils redisUtils = new RedisUtils(host,port,auth);
            if (redisUtils.jedis.ping().equals("PONG")){
                msg = "连接成功！";
                redisUtils.close();
            }
            JOptionPane.showMessageDialog(null,msg);
        });
        //保存按钮监听器
        saveBtn.addActionListener(e -> {
            String name = nameField.getText();
            String host = hostField.getText();
            String port = portField.getText();
            String auth = authField.getText();
            //读取配置文件，查看是否有同名
            Map<String,String> cache = new HashMap<>();
            PropertiesUtil info = new PropertiesUtil("INFO.properties");
            List<String> keys = info.getAllKeys();
            for (String key : keys){
                cache.put(key, info.get(key));
            }
            if (cache.containsKey(name)){
                //有同名则弹窗警告
                JOptionPane.showMessageDialog(null,"已存在同名连接！");
            }else{
                String url = host + ";" + port + ";" + auth;
                cache.put(name,url);
                info.update(cache,"INFO.properties");
                redisTool.addTree(name, url, info.getAllKeys().size());
                redisTool.refreshFrame();
                cleanFrame();
                this.dispose();
            }
        });

    }

    /**
     * 清空jframe
     */
    private void cleanFrame(){
        nameField.setText("");
        hostField.setText("");
        portField.setText("");
        authField.setText("");
    }

}
